package cn.code.auth.authorize.web;


import cn.code.auth.authorize.constant.constant;
import cn.code.auth.authorize.dao.AuthTbMenuMapper;
import cn.code.auth.authorize.dto.ExceptionMessage;
import cn.code.auth.authorize.dto.Message;
import cn.code.auth.authorize.model.AuthTbMenu;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import io.swagger.annotations.ApiOperation;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;

/**
 * Created by wangYuDong on 2017/5/10.
 */
@Controller
@RequestMapping("/authTbMenu")
public class AuthTbMenuController {
    @Resource
    private AuthTbMenuMapper authTbMenuMapper;

    @ResponseBody
    @RequestMapping(value = "/get", method = RequestMethod.GET)
    public Page<AuthTbMenu> getUserList(Page<AuthTbMenu> page) {
        page.setRecords(authTbMenuMapper.selectMyPage(page, new EntityWrapper<AuthTbMenu>()));
        return page;
    }

    @ResponseBody
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public Message authTbMenuAdd(AuthTbMenu authTbMenu) {
        Integer i = authTbMenuMapper.insert(authTbMenu);
        if (i > 0) {
            return new Message("添加成功");
        } else {
            return new ExceptionMessage("添加失败", constant.ERROR_USER + constant.ERROR_CREATE);
        }
    }

    @ResponseBody
    @RequestMapping(value = "/{id}", method = RequestMethod.DELETE)
    public Message authTbMenuDelete(@PathVariable Integer id) {
        Integer i = authTbMenuMapper.deleteById(id);
        if (i > 0) {
            return new Message("删除成功");
        } else {
            return new ExceptionMessage("删除失败", constant.ERROR_USER + constant.ERROR_DELETE);
        }
    }

    @ResponseBody
    @RequestMapping(value = "/{id}", method = RequestMethod.PATCH)
    @ApiOperation(value = "修改", httpMethod = "PATCH")
    public Message authTbMenuUpdate(@PathVariable Integer id, AuthTbMenu authTbMenu) {
        authTbMenu.setId(id);
        Integer i = authTbMenuMapper.updateById(authTbMenu);
        if (i > 0) {
            return new Message("更新成功");
        } else {
            return new ExceptionMessage("更新失败", constant.ERROR_USER + constant.ERROR_UPDATE);
        }
    }
}
